﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
//需要添加以下命名空间
using UFIDA.U8.MomServiceCommon;
using UFIDA.U8.U8MOMAPIFramework;
using UFIDA.U8.U8APIFramework;
using UFIDA.U8.U8APIFramework.Meta;
using UFIDA.U8.U8APIFramework.Parameter;
using MSXML2;
using System.Runtime.InteropServices;
using ERPU8WebApi.Log;
using ERPU8WebApi.App_Start;
using ERPU8WebApi.Models;
using ERPU8WebApi.Dal;
using System.Data;
using System.IO;
using ERPU8WebApi.Until;
using ERPU8WebApi.Models.Procurement;
using ERPU8WebApi.Models.Sys;

namespace ERPU8WebApi.Controllers
{
    public class SysController : ApiControllerBase
    {
        //查询U8用户列表
        [Route("U8API/SysController/LoadU8User")]
        [HttpPost]
        public IHttpActionResult LoadU8User(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string userId = m.userId;
                DALSys s = new DALSys();
                U8UserController scc = new U8UserController();
                List<U8User> scList = new List<U8User>();
                DataTable scDt = s.GetUserList(userId);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    U8User sNew = new U8User();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询U8用户列表成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询地区
        [Route("U8API/SysController/LoadDistrictClass")]
        [HttpPost]
        public IHttpActionResult LoadDistrictClass(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cDCCode = m.cDCCode;
                DALSys s = new DALSys();
                DistrictClassController scc = new DistrictClassController();
                List<DistrictClassModel> scList = new List<DistrictClassModel>();
                DataTable scDt = s.GetDistrictClass(cDCCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    DistrictClassModel sNew = new DistrictClassModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询地区成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询客户
        [Route("U8API/SysController/LoadCustomer")]
        [HttpPost]
        public IHttpActionResult LoadCustomer(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cCusCode = m.cCusCode;
                DALSys s = new DALSys();
                CustomerController scc = new CustomerController();
                List<CustomerModel> scList = new List<CustomerModel>();
                DataTable scDt = s.GetCustomer(cCusCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    CustomerModel sNew = new CustomerModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询客户档案成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询银行
        [Route("U8API/SysController/LoadBank")]
        [HttpPost]
        public IHttpActionResult LoadBank(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cBankCode = m.cBankCode;
                DALSys s = new DALSys();
                BankController scc = new BankController();
                List<BankModel> scList = new List<BankModel>();
                DataTable scDt = s.GetBank(cBankCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    BankModel sNew = new BankModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询银行档案成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询部门
        [Route("U8API/SysController/LoadDepartment")]
        [HttpPost]
        public IHttpActionResult LoadDepartment(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cDepCode = m.cDepCode;
                DALSys s = new DALSys();
                DepartmentController scc = new DepartmentController();
                List<DepartmentModel> scList = new List<DepartmentModel>();
                DataTable scDt = s.GetDepartment(cDepCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    DepartmentModel sNew = new DepartmentModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询部门档案成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询业务员
        [Route("U8API/SysController/LoadPerson")]
        [HttpPost]
        public IHttpActionResult LoadPerson(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cPersonCode = m.cPersonCode;
                DALSys s = new DALSys();
                PersonController scc = new PersonController();
                List<PersonModel> scList = new List<PersonModel>();
                DataTable scDt = s.GetPerson(cPersonCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    PersonModel sNew = new PersonModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询业务员成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询仓库
        [Route("U8API/SysController/LoadWarehouse")]
        [HttpPost]
        public IHttpActionResult LoadWarehouse(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cWhCode = m.cWhCode;
                DALSys s = new DALSys();
                WarehouseController scc = new WarehouseController();
                List<WarehouseModel> scList = new List<WarehouseModel>();
                DataTable scDt = s.GetWarehouse(cWhCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    WarehouseModel sNew = new WarehouseModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询仓库成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询币种
        [Route("U8API/SysController/LoadForeigncurrency")]
        [HttpPost]
        public IHttpActionResult LoadForeigncurrency(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cexchName = m.cexchName;
                DALSys s = new DALSys();
                ForeigncurrencyController scc = new ForeigncurrencyController();
                List<ForeigncurrencyModel> scList = new List<ForeigncurrencyModel>();
                DataTable scDt = s.GetForeigncurrency(cexchName);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    ForeigncurrencyModel sNew = new ForeigncurrencyModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询币种成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }

        //查询结算方式
        [Route("U8API/SysController/LoadSettleStyle")]
        [HttpPost]
        public IHttpActionResult LoadSettleStyle(dynamic m)
        {
            var log = LogFactory.GetLogger(this.GetType().ToString());
            string logMessage = "";
            try
            {
                string cSSCode = m.cSSCode;
                DALSys s = new DALSys();
                SettleStyleController scc = new SettleStyleController();
                List<SettleStyleModel> scList = new List<SettleStyleModel>();
                DataTable scDt = s.GetSettleStyle(cSSCode);
                for (int i = 0; i < scDt.Rows.Count; i++)
                {
                    SettleStyleModel sNew = new SettleStyleModel();
                    sNew = scc.GetModel(scDt.Rows[i]);
                    scList.Add(sNew);
                }
                logMessage = "查询结算方式成功";
                return IHttpSuccess(scList);
            }
            catch (Exception ex)
            {
                logMessage = ex.ToString();
                return IHttpError(Models.ResponseCode.OtherError, ex.ToString());
            }

            finally
            {
                string strMessage = log.ExeFormat(logMessage);
                log.Info(strMessage);
            }

        }
    }
}
